package com.sakura.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sakura.entity.SakuraUser;
import com.sakura.service.LoginService;

@Controller
@RequestMapping("login")
public class LoginController {
	
	@Autowired
	private LoginService loginService;

	@RequestMapping("/loginMain")
	public ModelAndView loginMainPage(String username, String password){
		ModelAndView modelAndView = new ModelAndView();
		boolean b = loginService.authUser(username, password);
		if (b) {
			modelAndView.setViewName("index");
		} else {
			modelAndView.setViewName("error");
		}
		return modelAndView;
	}
	
	@RequestMapping("/registerUser")
	public ModelAndView registerUser(HttpServletRequest request, SakuraUser sakuraUser){
		ModelAndView modelAndView = new ModelAndView("success");
		try {
			loginService.registerUser(sakuraUser);
		    modelAndView.addObject("sakuraUser", sakuraUser);
		    modelAndView.addObject("path", request.getContextPath());	
		    modelAndView.setViewName("success");
		} catch (Exception e) {
			modelAndView.setViewName("field");
			e.printStackTrace();
		}
		return modelAndView;
	}
	
	
}
